package jvm;

import org.junit.jupiter.api.Test;

import java.lang.management.*;
import java.util.List;

/**
 * Author: hu
 * Date: 2022/7/26 10:24
 */
public class JvmDemo {

    /**
     *  Author: hu
     *  Date  : 2022/7/26 10:26
     *获取进程ID
     */
    @Test
    public void test1() {
        RuntimeMXBean mxb = ManagementFactory.getRuntimeMXBean();
        System.out.println(mxb.getName());//4620@cs-hu
        System.out.println(mxb.getClassPath());
        System.out.println(mxb.getInputArguments());
    }
    /**
     *  Author: hu
     *  Date  : 2022/7/26 10:26
     *获取操作系统信息
     */
    @Test
    public void test2() {
        OperatingSystemMXBean osb = ManagementFactory.getOperatingSystemMXBean();
        System.out.println(osb.getName()); //Windows 10
        System.out.println(osb.getArch()); //amd 64
        System.out.println(osb.getAvailableProcessors()); //4
        System.out.println(osb.getVersion()); //10.0
        System.out.println(osb.getSystemLoadAverage()); //-1.0

    }
    /**
     *  Author: hu
     *  Date  : 2022/7/26 10:31
     *获取jvm 内存信息
     */
    @Test
    public void test4() {
        MemoryMXBean mxb = ManagementFactory.getMemoryMXBean();
        MemoryUsage mu = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
        //Heap
        System.out.println("Max:" + mxb.getHeapMemoryUsage().getMax() / 1024 / 1024 + "MB");    //Max:1776MB
        System.out.println("Init:" + mxb.getHeapMemoryUsage().getInit() / 1024 / 1024 + "MB");  //Init:126MB
        System.out.println("Committed:" + mxb.getHeapMemoryUsage().getCommitted() / 1024 / 1024 + "MB");   //Committed:121MB
        System.out.println("Used:" + mxb.getHeapMemoryUsage().getUsed() / 1024 / 1024 + "MB");  //Used:7MB
        System.out.println(mxb.getHeapMemoryUsage().toString());    //init = 132120576(129024K) used = 8076528(7887K) committed = 126877696(123904K) max = 1862270976(1818624K)


        //Non heap
        System.out.println("Max:" + mxb.getNonHeapMemoryUsage().getMax() / 1024 / 1024 + "MB");    //Max:0MB
        System.out.println("Init:" + mxb.getNonHeapMemoryUsage().getInit() / 1024 / 1024 + "MB");  //Init:2MB
        System.out.println("Committed:" + mxb.getNonHeapMemoryUsage().getCommitted() / 1024 / 1024 + "MB");   //Committed:8MB
        System.out.println("Used:" + mxb.getNonHeapMemoryUsage().getUsed() / 1024 / 1024 + "MB");  //Used:7MB
        System.out.println(mxb.getNonHeapMemoryUsage().toString());    //init = 2555904(2496K) used = 7802056(7619K) committed = 9109504(8896K) max = -1(-1K)
    }
    /**
     *  Author: hu
     *  Date  : 2022/7/26 10:32
     *获取内存池情况
     */
    @Test
    public void test5() {
        List<MemoryPoolMXBean> mxb = ManagementFactory.getMemoryPoolMXBeans();
        for (MemoryPoolMXBean memoryPoolMXBean : mxb) {
            System.out.println("Name:" + memoryPoolMXBean.getName());
            System.out.println("Usage:" + memoryPoolMXBean.getUsage());
            System.out.println("Manager:" + String.join(",", memoryPoolMXBean.getMemoryManagerNames()));
            System.out.println("Type:" + memoryPoolMXBean.getType());
            System.out.println("--------------------------");
        }

    }
}
